using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using AppPriceLive.Entities.Models;
using AppPriceLive.Entities.Utils;
using AppPriceLive.Common;

namespace AppPriceLive.DAL.Base
{
	public class ImagePRDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public ImagePRDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the ImagePR table.
		/// </summary>
		public virtual void Insert(ImagePRInfo imagePRInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EffDate", imagePRInfo.EffDate),
				new SqlParameter("@EndDate", imagePRInfo.EndDate),
				new SqlParameter("@Url", imagePRInfo.Url),
				new SqlParameter("@UrlReference", imagePRInfo.UrlReference),
				new SqlParameter("@InsertDate", imagePRInfo.InsertDate),
				new SqlParameter("@UserName", imagePRInfo.UserName)
			};

			imagePRInfo.ImgId = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ImagePR_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the ImagePR table.
		/// </summary>
		public virtual void Update(ImagePRInfo imagePRInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ImgId", imagePRInfo.ImgId),
				new SqlParameter("@EffDate", imagePRInfo.EffDate),
				new SqlParameter("@EndDate", imagePRInfo.EndDate),
				new SqlParameter("@Url", imagePRInfo.Url),
				new SqlParameter("@UrlReference", imagePRInfo.UrlReference),
				new SqlParameter("@InsertDate", imagePRInfo.InsertDate),
				new SqlParameter("@UserName", imagePRInfo.UserName)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ImagePR_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the ImagePR table by its primary key.
		/// </summary>
		public virtual void Delete(int? imgId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ImgId", imgId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ImagePR_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the ImagePR table.
		/// </summary>
		public virtual ImagePRInfo Select(int? imgId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@ImgId", imgId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ImagePR_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeImagePRInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        public virtual ImagePRInfo SelectDateQuery(DateTime DateQuery)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@DateQuery", DateQuery)
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ImagePR_SelectDateQuery", parameters))
			{
				if (dataReader.Read())
				{
					return MakeImagePRInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}
		/// <summary>
		/// Selects all records from the ImagePR table.
		/// </summary>
		public List<ImagePRInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ImagePR_SelectAll"))
			{
				List<ImagePRInfo> imagePRInfoList = new List<ImagePRInfo>();
				while (dataReader.Read())
				{
					imagePRInfoList.Add(MakeImagePRInfo(dataReader));
				}

				return imagePRInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the ImagePR class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual ImagePRInfo MakeImagePRInfo(SqlDataReader dataReader)
		{
			ImagePRInfo imagePRInfo = new ImagePRInfo();
			imagePRInfo.ImgId = SqlClientUtility.GetInt32(dataReader, DbConstants.IMAGEPR.IMGID);
			imagePRInfo.EffDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.IMAGEPR.EFFDATE);
			imagePRInfo.EndDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.IMAGEPR.ENDDATE);
			imagePRInfo.Url = SqlClientUtility.GetString(dataReader, DbConstants.IMAGEPR.URL);
			imagePRInfo.UrlReference = SqlClientUtility.GetString(dataReader, DbConstants.IMAGEPR.URLREFERENCE);
			imagePRInfo.InsertDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.IMAGEPR.INSERTDATE);
			imagePRInfo.UserName = SqlClientUtility.GetString(dataReader, DbConstants.IMAGEPR.USERNAME);

			return imagePRInfo;
		}

		#endregion
	}
}
